//
//  ZYDatePickerView.m
//  YiLin
//
//  Created by Zyark on 2018/6/21.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYDatePickerView.h"

@interface ZYDatePickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong) UIPickerView *pickerView;
@end

@implementation ZYDatePickerView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    [self addSubview:_pickerView];
    _pickerView.center = self.center;
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
}

#pragma mark UIPickerViewDelegate DataSource
//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

//返回指定列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component == 0){
        return 24;
    }else{
        return 60;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(row < 10){
        return [NSString stringWithFormat:@"0%ld",row];
    }else{
        return [NSString stringWithFormat:@"%ld",row];
    }
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component == 1){
        _minute = row;
    }else{
        _hour = row;
    }
    if([_delegate respondsToSelector:@selector(datePickerView:didSelectWithHour:minute:)]){
        [_delegate datePickerView:self didSelectWithHour:_hour minute:_minute];
    }
}

#pragma mark Setter
-(void)setHour:(NSInteger)hour{
    _hour = hour;
    [_pickerView selectRow:hour inComponent:0 animated:YES];
}

-(void)setMinute:(NSInteger)minute{
    _minute = minute;
    [_pickerView selectRow:_minute inComponent:1 animated:YES];
}
@end
